package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"sleep-guard/cache"
	"sleep-guard/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")
	car_plate := c.PostForm("car_plate")

	data := models.Car{
		CarCode:  car_code,
		CarName:  car_name,
		CarPlate: car_plate,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_from := c.PostForm("order_from")
	order_to := c.PostForm("order_to")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	data := models.CarOrder{
		OrderCode: order_code,
		OrderFrom: order_from,
		OrderTo:   order_to,
		Cid:       cid,
	}

	err := models.DB.Create(&data).Error

	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarOrderList(c *gin.Context) {
	//SELECT
	//car_orders.id as OrderId,
	//cars.car_code as CarCode,
	//cars.car_name as CarName,
	//cars.car_plate as CarPlate,
	//car_orders.order_code as OrderCode,
	//car_orders.order_from as OrderFrom,
	//car_orders.order_to as OrderTo
	//FROM
	//car_orders
	//LEFT JOIN cars ON cars.id = car_orders.cid

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var list []models.CarOrderList

	redis_key := "car_order_list_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select(
				"car_orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_plate as CarPlate",
				"car_orders.order_code as OrderCode",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
			).
			Offset(offset).
			Limit(size).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Debug().
			Find(&list)

		jsonData, _ := json.Marshal(list)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  list,
		"total": total,
	})
	return

}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")

	var detail models.CarOrderList

	redis_key := "car_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select(
				"car_orders.id as OrderId",
				"cars.car_code as CarCode",
				"cars.car_name as CarName",
				"cars.car_plate as CarPlate",
				"car_orders.order_code as OrderCode",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id = ?", id).
			Debug().
			Find(&detail)

		jsonData, _ := json.Marshal(detail)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &detail)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": detail,
	})
	return

}
